<?php
// -----------------------------------------------------------------------------
// include
// -----------------------------------------------------------------------------

include "../config/config.php";
include "../auth.php";
include "../DBManager.php";

// -----------------------------------------------------------------------------
// 访问设置
// -----------------------------------------------------------------------------

if (!isset($_GET["categoryId"]) || !isset($_GET["linkId"])) {
    exit("需要传入有效的参数！");
}

$categoryId = $_GET["categoryId"];
$specifiedLinkId = $_GET["linkId"];

// -----------------------------------------------------------------------------
// 数据库相关设置
// -----------------------------------------------------------------------------

$dbPath = "../" . $dbPath;
$db = new DBManager("sqlite:" . $dbPath);

// -----------------------------------------------------------------------------

$links = $db->getListOfLinksInCategory($categoryId, "sortNumber");

if (count($links) > 0) {

    echo <<<BBB
        <div class="list-group">
    BBB;

    foreach ($links as $link) {

        $linkId = $link["id"];
        $url = $link["url"];
        $sortNumber = $link["sortNumber"];
        $categoryId = $link["categoryId"];

        $title = $link["title"];
        $linkColor = "";
        if ($linkId == $specifiedLinkId) {
            $linkColor = "list-group-item-info";
        }

        $timesOfClick = $link["timesOfClick"];
        if (is_null($timesOfClick)) {
            $timesOfClick = 0;
        }

        echo <<<BBB
            <a href="$url" 
               class="list-group-item list-group-item-action d-flex align-items-center openLink {$linkColor}"
               id="$linkId">
               
               <span class="w-100 text-break">$title</span>
               <span class="flex-shrink-0">
                   <span class="badge rounded-pill text-bg-light">{$timesOfClick}点击</span>
                   <span data-id="{$linkId}" 
                       data-title="{$title}" 
                       data-sortnumber="{$sortNumber}" 
                       class="badge text-bg-dark btnManageLink">管理</span>
               </span>

            </a>
        BBB;

    }

    echo <<<BBB
        </div>
    BBB;


} else {
    echo "<p class='fw-light text-muted'>暂无，请先创建。</p>";
}
